/*------------------------------------------------------------------------/
/  The Main Development Bench of TJpgDec Module 
/-------------------------------------------------------------------------/
/
/  Copyright (C) 2011, ChaN, all right reserved.
/
/ * This software is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/-------------------------------------------------------------------------*/

#include "ff.h"
#include "diskio.h"
#include "lcd_driver.h"
#include "tjpgd.h"



static uint16_t _sx,_sy;      //start point x,y
static uint32_t filecur=0;
/* User defined session identifier */


typedef struct {
	FIL   hin;		/* Handle to input stream */
//	BYTE *frmbuf;	/* Pointer to the frame buffer */
//	DWORD wbyte;	/* Number of bytes a line of the frame buffer */
} IODEV;



/* User defined input function */

uint16_t input_func (
	JDEC* jd,		/* Decompression object */
	uint8_t* buff,		/* Pointer to the read buffer (0:skip) */
	uint16_t ndata		/* Number of bytes to read/skip */
)
{
	uint32_t rb;
	uint32_t temp;
	
	IODEV *dev = (IODEV*)jd->device;

	if (buff) 
	{
		f_read(&dev->hin, buff, ndata, &rb);
		return rb;
	} else 
	{
		filecur = dev->hin.fptr + ndata;
		temp = f_lseek(&dev->hin, filecur);
		return temp? 0 : ndata;
	}
}



/* User defined output function */

uint16_t output_func (
	JDEC* jd,		/* Decompression object */
	void* bitmap,	/* Bitmap data to be output */
	JRECT* rect		/* Rectangular region to output */
)
{
	uint8_t *src;
	IODEV *dev = (IODEV*)jd->device;


	/* Put progress indicator */
	if (rect->left == 0) 
	{
		PRINTF("%u%%", (rect->top << jd->scale) * 100UL / jd->height);
	}
    src = (uint8_t*)bitmap;
    //lcd_show_image(rect->left + _sx,rect->top + _sy,(rect->right - rect->left)+1,(rect->bottom - rect->top)+1,src);
    lcd_fill_buff(rect->left + _sx, rect->top + _sy, rect->right  + _sx,rect->bottom + _sy,src);

	return 1;	/* Continue to decompress */
}

IODEV devid; /* User defined device identifier */
int Decode_Jpg(uint8_t *file_name,uint16_t x,uint16_t y)
{
    void *work;  /* Pointer to the decompressor work area */
    JDEC jdec;   /* Decompression object */
    JRESULT res; /* Result code of TJpgDec API */
	uint8_t scale;


	/* Open a JPEG file */
    if (f_open(&devid.hin,(char*)file_name, FA_READ|FA_OPEN_ALWAYS) != FR_OK ) return -1;

    _sx = x;
    _sy = y;

    /* Allocate a work area for TJpgDec */
    work = malloc(3200);
    if(work == NULL) return -1;

    /* Prepare to decompress */
    res = jd_prepare(&jdec, input_func, work, 3200, &devid);
    if (res == JDR_OK) 
	{
		if((jdec.width/LCD_W)>(jdec.height/LCD_H))
		{
			scale = (jdec.width-1)/LCD_W;
		}else
		{
			scale = (jdec.height-1)/LCD_H;
		}		
		PRINTF("W:H=%d * %d,Scale=%d\n", jdec.width,jdec.height,scale);
		
        /* Ready to dcompress. Image info is available here. */
        res = jd_decomp(&jdec, output_func, scale);   /* Start to decompress with 1/2 scaling */
            
        if (res == JDR_OK) {
            /* Decompression succeeded. You have the decompressed image in the frame buffer here. */
            PRINTF("\rOK  \n");
        } else 
		{
            PRINTF("Failed to decompress: rc=%d\n", res);
        }
    } else 
	{
        PRINTF("Failed to prepare: rc=%d\n", res);
    }
    free(work);             	/* Discard work area */
    f_close(&devid.hin);       /* Close the JPEG file */
    return res;
}


